package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.IUserDao;
import com.cn.wanxi.model.PageModel;
import com.cn.wanxi.model.UserModel;
import com.cn.wanxi.util.GetSqlSession;
import com.cn.wanxi.util.JDBC;

import javax.jws.soap.SOAPBinding;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class UserImpl implements IUserDao {
    @Override
    public int add(UserModel userModel) {
//        String sql = "insert into user(username,password,phone,sex,birthday,hobby,introduce) " +
//                "values('" + userModel.getUsername() + "'," +
//                "'" + userModel.getPassword() + "'," +
//                "'" + userModel.getPhone() + "'," +
//                "'" + userModel.getSex() + "'," +
//                "'" + userModel.getBirthday() + "'," +
//                "'" + userModel.getHobby() + "'," +
//                "'" + userModel.getIntroduce() + "')";
//        return JDBC.excuteUpdate(sql);
        return GetSqlSession.getSqlSession().insert("com.cn.wanxi.dao.IUserDao.add", userModel);
    }

    @Override
    public boolean findByUsername(String username) {
        UserModel userModel = GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.IUserDao.findByUsername", username);
        return userModel != null;
    }

    /**
     * 根据用户名和密码查询数据
     *
     * @param userModel
     * @return
     */
    @Override
    public boolean findByUsernameAndPassword(UserModel userModel) {
        UserModel userModel1 = GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.IUserDao.findByUsernameAndPassword", userModel);
        return userModel1 != null;
    }

    @Override
    public List<UserModel> findAll(UserModel userModel) {

        return GetSqlSession.getSqlSession().selectList("com.cn.wanxi.dao.IUserDao.findAll", userModel);
    }

    @Override
    public int del(int id) {
//        String sql = "delete from user where id=" + id;
//        return JDBC.excuteUpdate(sql);
        return GetSqlSession.getSqlSession().delete("com.cn.wanxi.dao.IUserDao.del", id);
    }

    @Override
    public UserModel findById(int id) {

        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.IUserDao.findById", id);
    }

    @Override
    public int edit(UserModel userModel) {
        return GetSqlSession.getSqlSession().update("com.cn.wanxi.dao.IUserDao.edit", userModel);
    }

    @Override
    public int getCount(UserModel userModel) {

        return GetSqlSession.getSqlSession().selectOne("com.cn.wanxi.dao.IUserDao.getCount", userModel);
    }
}
